/*
	This file is part of Erosion, a clone of Qix
	Copyright (c) 2009 Bill Whitacre

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "draw.h"
#include <math.h>

static float _r,_g,_b,_a;
int rat_pretty_draw=0;

void rat_line_width(float width)
{
	glLineWidth(width);
}

void rat_point_size(float size)
{
	glPointSize(size);
}

void rat_set_color(float r,float g,float b,float a)
{
	glColor4f(_r=r,_g=g,_b=b,_a=a);
}

void rat_get_color(float *r,float *g,float *b,float *a)
{
	*r=_r; *g=_g;
	*b=_b; *a=_a;
}

void rat_draw_point(float x,float y)
{
	glBegin(GL_POINTS);
		glVertex2d(x,y);
	glEnd();
}

void rat_draw_points(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_POINTS);
	{
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	}
	glEnd();
}

void rat_draw_line(float x1,float y1,float x2,float y2)
{
	glBegin(GL_LINES);
	{
		glVertex2d(x1,y1);
		glVertex2d(x2,y2);
	}
	glEnd();
}

void rat_draw_lines(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_LINES);
	{
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	}
	glEnd();
}

void rat_draw_line_strip(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_LINE_STRIP);
	{
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	}
	glEnd();
}

void rat_draw_line_loop(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_LINE_LOOP);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_quads(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_QUADS);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_triangles(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_TRIANGLES);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_triangle_fan(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_TRIANGLE_FAN);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_triangle_strip(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_TRIANGLE_STRIP);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_rect(float x1,float y1,float x2,float y2,int fill)
{
	if (fill)
		glBegin(GL_QUADS);
	else
		glBegin(GL_LINE_LOOP);
	{
		glVertex2d(x1,y1);
		glVertex2d(x1,y2);
		glVertex2d(x2,y2);
		glVertex2d(x2,y1);
	} glEnd();
}

#ifndef _MSC_VER
#	define cos cosf
#	define sin sinf
#endif

void rat_draw_circle(float x,float y,float radius,float stepdivisor,int fill)
{
	float theta,xmod,ymod,step=MATH_PI/(4.0f*radius*stepdivisor);
	step=step>MATH_PI/4.0f?MATH_PI/4.0f:step;

	if (fill)
	{
		glBegin(GL_POLYGON);
			for (theta=0.0f; theta<=MATH_PI*2.0f; theta+=step)
			{
				xmod=cos(theta)*radius;
				ymod=sin(theta)*radius;
				glVertex2f(x+xmod,y+ymod);
			}
		glEnd();
	}
	else
	{
		glBegin(GL_LINE_STRIP);
			for (theta=0.0f; theta<MATH_PI*2.0f; theta+=step)
			{
				xmod=cos(theta)*radius;
				ymod=sin(theta)*radius;
				glVertex2f(x+xmod,y+ymod);
			}
			glVertex2f(x+xmod,y+ymod);
			glVertex2f(x+cos(0)*radius,y+sin(0)*radius);
		glEnd();
	}
}

void rat_draw_elipse(float x,float y,float rx,float ry,float stepdivisor,int fill)
{
	float theta,xmod,ymod,step=MATH_PI/(4.0f*(rx<ry?rx:ry)*stepdivisor);

	if (fill)
	{
		glBegin(GL_POLYGON);
			for (theta=0.0f; theta<=MATH_PI*2.0f; theta+=step)
			{
				xmod=cos(theta)*rx;
				ymod=sin(theta)*ry;
				glVertex2f(x+xmod,y+ymod);
			}
		glEnd();
	}
	else
	{
		glBegin(GL_LINE_STRIP);
			for (theta=0.0f; theta<MATH_PI*2.0f; theta+=step)
			{
				xmod=cos(theta)*rx;
				ymod=sin(theta)*ry;
				glVertex2f(x+xmod,y+ymod);
			}
			glVertex2f(x+xmod,y+ymod);
			glVertex2f(x+cos(0)*rx,y+sin(0)*ry);
		glEnd();
	}
}

//	for passes:
//		0 - first pass: fill
//		1 - second pass: lines
//		2 - all passes: do all
void rat_draw_body(rat_body *body,int pass)
{
	rat_real r,g,b,a;
	vector2 pos=rat_hull_get_pos(body->hull);
	rat_real angle=rat_hull_get_angle(body->hull);

	switch (body->hull->type)
	{
		case rat_hull_type_polygon:
		{
			register unsigned int i;
			rat_hull_polygon *hull_polygon=(rat_hull_polygon *)body->hull;

			rat_get_color(&r,&g,&b,&a);
			if (pass==0||pass==2)
			{
				rat_set_color(0.0,0.0,0.25,0.5);
			
				rat_draw_triangle_fan((rat_real *)hull_polygon->world_verts,hull_polygon->numverts);
			}
			
			rat_set_color(r,g,b,a);
			if (pass==1||pass==2)
			{
				rat_draw_line_loop((rat_real *)hull_polygon->world_verts,hull_polygon->numverts);

				if (rat_pretty_draw)
				{
					for (i=0; i<hull_polygon->numverts; i++)
					{
						vector2 axisline_a;
						vector2 axisline_b;

						axisline_a=vector2_multf(vector2_add(hull_polygon->world_verts[i],hull_polygon->world_verts[(i+1)%hull_polygon->numverts]),0.5);
						axisline_b=vector2_add(axisline_a,vector2_multf(vector2_negative(hull_polygon->world_axes[i].normal),5.0));

						rat_draw_line(axisline_a.x,axisline_a.y,axisline_b.x,axisline_b.y);
					}
				}
			}
		} break;
		case rat_hull_type_circle:
		{
			const rat_real mpi2=MATH_PI/2.0;
			rat_hull_circle *hull_circle=(rat_hull_circle *)body->hull;
			
			rat_get_color(&r,&g,&b,&a);
			if (pass==0||pass==2)
			{
				rat_set_color(0.0,0.0,0.25,0.5);

				rat_draw_circle(pos.x,pos.y,hull_circle->radius,0.2f,1);
			}
			
			rat_set_color(r,g,b,a);
			if (pass==1||pass==2)
			{
				if (rat_pretty_draw)
				{
					rat_real subrad=hull_circle->radius>10?hull_circle->radius-5:hull_circle->radius*0.5;

					rat_real ang1cos=cos(angle)*hull_circle->radius;
					rat_real ang1sin=sin(angle)*hull_circle->radius;
					rat_real ang2cos=cos(angle+mpi2)*hull_circle->radius;
					rat_real ang2sin=sin(angle+mpi2)*hull_circle->radius;

					rat_real ang1cossub=cos(angle)*subrad;
					rat_real ang1sinsub=sin(angle)*subrad;
					rat_real ang2cossub=cos(angle+mpi2)*subrad;
					rat_real ang2sinsub=sin(angle+mpi2)*subrad;

					rat_draw_circle(pos.x,pos.y,hull_circle->radius,0.2f,0);

					rat_draw_line(
						pos.x+ang1cossub,pos.y+ang1sinsub,
						pos.x+ang1cos,pos.y+ang1sin);
					rat_draw_line(
						pos.x-ang1cossub,pos.y-ang1sinsub,
						pos.x-ang1cos,pos.y-ang1sin);
					rat_draw_line(
						pos.x+ang2cossub,pos.y+ang2sinsub,
						pos.x+ang2cos,pos.y+ang2sin);
					rat_draw_line(
						pos.x-ang2cossub,pos.y-ang2sinsub,
						pos.x-ang2cos,pos.y-ang2sin);
					rat_draw_point(pos.x,pos.y);
				}
				else
				{
					rat_real cosine=cos(angle)*hull_circle->radius;
					rat_real sine=sin(angle)*hull_circle->radius;

					rat_draw_circle(pos.x,pos.y,hull_circle->radius,0.2f,0);
					rat_draw_line(pos.x,pos.y,pos.x+cosine,pos.y+sine);
				}
			}
		} break;
		case rat_hull_type_particle:
		{
			if (pass==1||pass==2)
				rat_draw_point(pos.x,pos.y);
		} break;
	}
}

void rat_draw_constraint(rat_constraint *constraint)
{
	vector2 anch_a,anch_b,length;

	switch (constraint->type)
	{
	case rat_constraint_type_bar:
		anch_a=vector2_add(
			((rat_constraint_bar *)constraint)->world_anch_a,
			rat_hull_get_pos(constraint->body_a->hull));
		anch_b=vector2_add(
			((rat_constraint_bar *)constraint)->world_anch_b,
			rat_hull_get_pos(constraint->body_b->hull));

		rat_draw_line(anch_a.x,anch_a.y,anch_b.x,anch_b.y);
		break;
	case rat_constraint_type_slider:
		anch_a=vector2_add(
			((rat_constraint_slider *)constraint)->world_anch_a,
			rat_hull_get_pos(constraint->body_a->hull));
		anch_b=vector2_add(
			((rat_constraint_slider *)constraint)->world_anch_b,
			rat_hull_get_pos(constraint->body_b->hull));

		rat_draw_line(anch_a.x,anch_a.y,anch_b.x,anch_b.y);
		break;
	}
}

void rat_draw_spring(rat_spring *spring)
{
	vector2 anch_a,anch_b;

	anch_a=vector2_add(
		vector2_getrotated(spring->anch_a,rat_hull_get_angle(spring->body_a->hull)),
		rat_hull_get_pos(spring->body_a->hull));
	anch_b=spring->body_b==NULL?spring->anch_b:vector2_add(
		vector2_getrotated(spring->anch_b,rat_hull_get_angle(spring->body_b->hull)),
		rat_hull_get_pos(spring->body_b->hull));

	rat_draw_line(anch_a.x,anch_a.y,anch_b.x,anch_b.y);
}
